URI无效:无法确定URI的格式。 [英] Invalid URI: the format of the URI could not be determined.

查看:272
本文介绍了URI无效:无法确定URI的格式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了Asp.Net Identity并且我实现了重置密码,我发送了一个链接到用户Email以重置密码。



这是代码:



 [HttpPost] 
[路线(ForgotPassword)]
[AllowAnonymous]
公共异步任务< IHttpActionResult> ; ForgotPassword(ForgotPasswordViewModel模型)
{
if(ModelState.IsValid)
{
var user = await UserManager.FindByEmailAsync(model.Email);
if(user == null ||!(await UserManager.IsEmailConfirmedAsync(user.Id)))
{
返回BadRequest(用户不存在或您尚未确认您的电子邮件。);
}

try
{
//使用此链接发送电子邮件
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
Url.Link(DefaultApi,
new {controller =Account / ConfirmEmail,userId = user.Id,code = code});

string callbackUrl = Url.Link(DefaultApi,
new {controller =Account / ManageAccount / reset-password,userId = user.Id,code = code});
// string callbackUrl = Url.Link(Default,
// new {controller =User / ManageAccount / reset-password,userId = user.Id,code = code});
等待UserManager.SendEmailAsync(user.Id,重置密码,
请点击< a href = \+ callbackUrl +\> here< /重置密码A>中);
返回Ok();
}
catch(exception ex)
{
返回InternalServerError(ex);
}

}

返回BadRequest();
}

// GET api /账户/ ManageAccount
[AcceptVerbs(GET)]
[AllowAnonymous]
[路线(ManageAccount) )]
public IHttpActionResult ManageAccount(string id)
{
if(!String.IsNullOrEmpty(id))
{
string page = id +。html ;

return Redirect(page);
}
返回Redirect(Login.html);
}




// POST:/ Account / ResetPassword
[HttpPost]
[AllowAnonymous]
[Route(ResetPassword)]
public async Task< IHttpActionResult> ResetPassword(ResetPasswordViewModel模型)
{
if(!ModelState.IsValid)
{
返回BadRequest(ModelState);
}
var user = await UserManager.FindByEmailAsync(model.Email);
if(user == null)
{
// return Redirect(https:// localhost:44342 / Login.html);
}
var result = await UserManager.ResetPasswordAsync(user.Id,model.Code,model.Password);
if(result.Succeeded)
{
返回Ok();
}
返回InternalServerError();
}







这是webApiConfig:



 public static class WebApiConfig 
{
public static void Register(HttpConfiguration config)
{
// Web API配置和服务
//将Web API配置为仅使用承载令牌身份验证。
config.SuppressDefaultHostAuthentication();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new
System.Net.Http.Headers.MediaTypeHeaderValue(application / json));
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));


// Web API路由
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name:DefaultApi,
routeTemplate:api / {controller} / {id},
defaults:new { id = RouteParameter.Optional}
);

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);


}
}





我的尝试:



在AccountController中,我尝试了这个但它不起作用:



 [HttpGet] 
[AllowAnonymous]
public async任务< IHttpActionResult> ManageAccount(string id)
{
if(!String.IsNullOrEmpty(id))
{
string page = id +。html;

return Redirect(page);
}
返回Redirect(Login.html);
}









此外,

 [AcceptVerbs(GET)] 
[AllowAnonymous]
[路线(ManageAccount / {id})]
public异步任务< IHttpActionResult> ManageAccount(string id)
{
if(!String.IsNullOrEmpty(id))
{
string page = id +。html;

return Redirect(page);
}
返回Redirect(Login.html);
}





这是发送到电子邮件的链接:(看看它的格式)!

 http: //   localhost:7524 / api / Account / ManageAccount ?/复位密码帐户= 1011&安培;代码= vbGi%2FzN0oFjw6RLlFVuBHiyEz2rH%2FNaO7tc5Y7Y47vzKKC5aNgx9yzZLbHtMD1%2BVZYCot1dvRZSLupPUYcxpCW%2FIl4cJwAIxVjVYA1kxrIjobdrXVqHNMXJmTF5u6cc%2FJdA0uDlQzNjoG4%2Fcjfl3ToRxarZokxI3VN8TEvt1I2M%3D  



当我点击它我得到了:

无效的URI:无法确定URI的格式。



我需要的是调用html页面with url具有userId和代码验证来自WebApi



我真的被困了你可以帮忙吗?

解决方案

< blockquote>你是否想要实现这样的东西? 忘记密码并重设电子邮件密码链接MVC [ ^ ]



请注意,MVC和Web API几乎相同,只是MVC提供视图但概念是一样的。


I used Asp.Net Identity and I implemented reset password, I send a link to user Email to reset password.

Here is the code:

[HttpPost]
    [Route("ForgotPassword")]
    [AllowAnonymous]
    public async Task<IHttpActionResult> ForgotPassword(ForgotPasswordViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindByEmailAsync(model.Email);
            if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
            {
             return BadRequest("Either user does not exist or you have not confirmed your email.");
            }

            try
            {
                // Send an email with this link
                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                Url.Link("DefaultApi",
              new { controller = "Account/ConfirmEmail", userId = user.Id, code = code });

                  string callbackUrl = Url.Link("DefaultApi", 
                    new { controller = "Account/ManageAccount/reset-password", userId = user.Id, code = code });
                //string callbackUrl = Url.Link("Default", 
                  //  new { controller = "User/ManageAccount/reset-password", userId = user.Id, code = code });
                await UserManager.SendEmailAsync(user.Id, "Reset Password", 
                    "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
                return Ok();
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }

        }

        return BadRequest();
    }

    // GET api/Account/ManageAccount
    [AcceptVerbs("GET")]
    [AllowAnonymous]
     [Route("ManageAccount")]
    public IHttpActionResult ManageAccount(string id)
    {
        if (! String.IsNullOrEmpty(id))
        {
            string page = id + ".html";

           return Redirect(page);
        }
        return Redirect("Login.html");
    }




    // POST: /Account/ResetPassword
    [HttpPost]
    [AllowAnonymous]
    [Route("ResetPassword")]
    public async Task<IHttpActionResult> ResetPassword(ResetPasswordViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        var user = await UserManager.FindByEmailAsync(model.Email);
        if (user == null)
        {
           // return Redirect("https://localhost:44342/Login.html");
        }
        var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
        if (result.Succeeded)
        {
            return Ok();
        }
        return InternalServerError();
    }




Here is webApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new
        System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));


        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling =
            Newtonsoft.Json.PreserveReferencesHandling.Objects;
        config.Formatters.Remove(config.Formatters.XmlFormatter);


    }
}



What I have tried:

In AccountController I tried this but it doesn't work:

[HttpGet]
[AllowAnonymous]
public async Task<IHttpActionResult> ManageAccount(string id)
{
    if (! String.IsNullOrEmpty(id))
    {
        string page = id + ".html";

       return Redirect(page);
    }
    return Redirect("Login.html");
}





Also,

[AcceptVerbs("GET")]
[AllowAnonymous]
[Route("ManageAccount/{id}")]
public async Task<IHttpActionResult> ManageAccount(string id)
{
    if (! String.IsNullOrEmpty(id))
    {
        string page =  id + ".html";

        return Redirect(page);
    }
    return Redirect("Login.html");
}



Here is the link sent to email:(Look at its format) !

http://localhost:7524/api/Account/ManageAccount/reset-password?userId=1011&code=vbGi%2FzN0oFjw6RLlFVuBHiyEz2rH%2FNaO7tc5Y7Y47vzKKC5aNgx9yzZLbHtMD1%2BVZYCot1dvRZSLupPUYcxpCW%2FIl4cJwAIxVjVYA1kxrIjobdrXVqHNMXJmTF5u6cc%2FJdA0uDlQzNjoG4%2Fcjfl3ToRxarZokxI3VN8TEvt1I2M%3D


When I click on it I got:
Invalid URI: The format of the URI could not be determined.

What I need is call html page "with url that have userId and code authentication" from WebApi

I'm really stuck Could you please help?

解决方案

Are you trying to implement something like this? Forgot Password And Reset Password Link On Email In MVC[^]

Note that MVC and Web API is pretty much the same, except that MVC serves Views but the concept is the same.


这篇关于URI无效:无法确定URI的格式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆